package com.topscomm.mis.cbb.pm.service.abs;

import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mis.cbb.pm.api.ITaskEditDocService;
import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEditDocEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskBackRecordService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskEditDocService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskService;
import com.topscomm.mis.cbb.pm.util.FlowNode;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.List;
import java.util.Map;

@ConditionalOnMissingBean(ITaskEditDocService.class)
public abstract class AbstractTaskEditDocService implements ITaskEditDocService {
    @Autowired
    private ICbbPmTaskEditDocService cbbPmTaskEditDocService;

    @Override
    public Map<String, Object> insert(Map<String, Object> paramMap) {
        beforeInsert(paramMap);
        CbbPmTaskEditDocEntity cbbPmTaskEditDocEntity = new CbbPmTaskEditDocEntity();
        cbbPmTaskEditDocEntity.convertFromMap(paramMap);
        cbbPmTaskEditDocEntity = cbbPmTaskEditDocService.insert(cbbPmTaskEditDocEntity);
        Map<String, Object> dataMap = cbbPmTaskEditDocEntity.convertToMap();
        afterInsert(dataMap);
        return dataMap;
    }

    protected void beforeInsert(Map<String, Object> paramMap) throws ServiceException {
    }

    protected void afterInsert(Map<String, Object> paramMap) throws ServiceException {
    }


    @Override
    public void update(Map<String, Object> paraMap) {
        cbbPmTaskEditDocService.update(paraMap);
    }

    @Override
    public void delete(Map<String, Object> paraMap) {
        String ids = ConvertUtil.convertToString(paraMap.get("ids"));
        cbbPmTaskEditDocService.deleteByWhere(" id in (" + ids + ")");
    }

    @Override
    public Map<String, Object> load(Map<String, Object> paraMap) {
        return cbbPmTaskEditDocService.queryMapById(paraMap.get("id").toString());
    }

    @Override
    public Page list(Map<String, Object> paraMap, String sqlWhere) {
        Query query = new Query(paraMap);
        int totalCount = cbbPmTaskEditDocService.getCount(sqlWhere);
        List<Map<String, Object>> tempList = null;
        if (query.getPageSize() > 0) {
            tempList = cbbPmTaskEditDocService.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(),
                    query.getSidx(), query.getSord());
        } else {
            tempList = cbbPmTaskEditDocService.queryMapByWhere(sqlWhere);
        }
        return new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
    }

}
